package com.xjscrm.console.chat.core.impl.staff;

import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.cache.CacheHolder;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RStaffMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.chat.utils.ChannelUtils;
import com.xjscrm.console.dto.MerchatChildDeviceRelationDTO;
import com.xjscrm.console.dto.MerchatChildDeviceRelationQueryDTO;
import com.xjscrm.console.service.customer.merchat.MerchatChildDeviceRelationService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatStaffOfflineMsgHandler extends ChatBaseMsgHandler<RStaffMsg> {

    @Autowired
    private CacheHolder cacheHolder;

    @Autowired
    private WwDeviceService wwDeviceService;

    @Autowired
    private MerchatChildDeviceRelationService merchatChildDeviceRelationService;

    @Override
    public Class<RStaffMsg> msgClass() {
        return RStaffMsg.class;
    }

    @Override
    public boolean basic() {
        return true;
    }

    @Override
    public SResultMsg doHandle(RStaffMsg content, ChannelHandlerContext ctx) {

        Long merId = content.getMerId();
        Long staffId = content.getStaffId();
        try {
            cacheHolder.delStaffTag(staffId);
            Channel staffChannel = cacheHolder.getStaffChannel(staffId);
            if (Objects.isNull(staffChannel)) {
                return SResultMsg.success(content, "员工已下班成功, 请勿重复下班");
            }

            List<MerchatChildDeviceRelationDTO> staffRelDevices = getStaffRelDevices(content);
            if (ObjectUtils.isNotEmpty(staffRelDevices)) {
                List<Long> deviceUniqueIds = staffRelDevices.stream().map(MerchatChildDeviceRelationDTO::getRobotId).distinct().collect(Collectors.toList());
                List<WwDevice> devices = wwDeviceService.getByIds(merId, deviceUniqueIds);
                if (ObjectUtils.isNotEmpty(devices)) {
                    for (WwDevice device : devices) {
                        if (Objects.equals(device.getOpenChat(), 1)) {
                            cacheHolder.delDeviceStaff(device.getId(), staffId);
                        }
                    }
                }
            }
            cacheHolder.delStaffSessionCount(merId, staffId);
            cacheHolder.delStaffChannel(staffId);

            ChannelUtils.cleanStaff(ctx.channel());
            return SResultMsg.success(content, "员工下班成功", ChatMsgType.s_staff_offline);
        } catch (Exception e) {
            log.error("员工下班失败", e);
            throw new BusinessException("员工下班失败");
        }
    }

    protected List<MerchatChildDeviceRelationDTO> getStaffRelDevices(RStaffMsg content) {

        MerchatChildDeviceRelationQueryDTO query = new MerchatChildDeviceRelationQueryDTO();
        query.setMerId(content.getMerId());
        query.setChildMerchatId(content.getStaffId());

        return merchatChildDeviceRelationService.list(query);
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_staff_offline;
    }
}
